#include <stdlib.h>
#include<stdio.h>
#include "els.h"

#include "els_heap.h"
#include "els_mem.h"
#include "els_object.h"
#include "els_vmhost.h"


void *els_Mem_growaux (els_VmObj *L, void *block, size_t nelems,int inc, size_t size, const char *errormsg, size_t limit) {
  size_t newn = nelems+inc;
  if (nelems >= limit-inc) els_error(L, errormsg);
  if ((newn ^ nelems) <= nelems ||  
       (nelems > 0 && newn < MINPOWER2))  
      return block;  
  else  
    return els_Mem_realloc(L, block, els_Object_power2(newn)*size);
}
void *els_Mem_realloc (els_VmObj *L, void *block, lint32 size) {
  if (size == 0) {  
    free(block);
    #ifdef ELS_DEBUG  
    printf("free\n");
    #endif
    return NULL;
  }
  #ifdef ELS_DEBUG
  printf("malloc\t%d\n",size);
  #endif
  block = realloc(block, size);
  if (block == NULL) {
    if (L) els_error(L, "内存管理器出错:无法获取足够的内存");  
    else return NULL;  
  }
  return block;
}


